const { Controller } = require('egg')
const Response = require('../utils/Response')
/**
 * @Controller  实验室模块
 */
class LabController extends Controller {

  // 收藏实验室
  /**
* @Router post /lab/collect-lab
* @Summary 收藏实验室
* @Description userId、laboratory 为必传项
* @Request body collectLab
* @apikey
*/
  async collectLab() {
    let { ctx, service } = this
    let { userId, laboratoryId } = ctx.request.body
    if (!userId || !laboratoryId) {
      ctx.body = new Response(400, "userId或laboratoryId不能为空", "")
    } else {
      let res = await service.laboratory.collectLab(userId, laboratoryId)
      if (res == "收藏成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")
      }
    }
  }

  // 创建实验室
  /**
* @Router post /lab/create-lab
* @Summary 创建实验室
* @Description labName、volume、address、type、status、intro、photo、selectTimes 为必传项
* @Request body createLab
* @apikey
*/
  async createLab() {
    let { ctx, service } = this
    let labInfo = ctx.request.body

    let { labName, volume, address, typeId, intro, photo,isExamine, selectTimes,eqList } = labInfo
    // 前端得判断可预约时间段至少有一个


    if (labName == undefined || volume == undefined || address == undefined || intro == undefined || photo == undefined || selectTimes == undefined||eqList ==undefined) {
      ctx.body = new Response(400, "缺少必填字段", "")
    } else {
      volume = parseInt(volume)
      typeId = parseInt(typeId)
      

      if (selectTimes.length == 0) {
        ctx.body = new Response(400, "预约可选时间段不可为空", "")
        return
      }
      if(eqList.length==0){
        ctx.body = new Response(400, "实验室设备不能为空", "")
        return
      }
      let res = await service.laboratory.createLab(labName, volume, address, typeId, intro, photo,isExamine, selectTimes,eqList)
      if (res == "创建成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")
      }
    }
  }
  // 更新实验室
  /**
* @Router post /lab/update-lab
* @Summary 更新实验室信息
* @Description laboratoryId/labName、volume、address、typeId、status、intro、photo、selectTimes 为必传项
* @Request body updateLab
* @apikey
*/
  async updateLab() {
    let { ctx, service } = this
    let { laboratoryId, labName, volume, address, typeId, status, intro, photo,isExamine, selectTimes,eqList }= ctx.request.body


    if (!laboratoryId || !labName || volume == undefined || address == undefined || typeId == undefined || intro == undefined || photo == undefined || selectTimes == undefined || eqList == undefined) {
      ctx.body = new Response(400, "缺少必填字段", "")
      return
    }

    // selectTimes = JSON.parse(selectTimes)
    // 前端得判断可预约时间段至少有一个
    // 为了防止前端不带上可预约时间 前端得判断不能一个时间段都没有哈
    if (selectTimes.length == 0) {
      ctx.body = new Response(400, "缺少可预约时间段", "")
      return
    } else {

      let res = await service.laboratory.updateLab(laboratoryId, labName, volume, address, typeId, intro, photo,  isExamine, selectTimes,eqList)
      if (res == "更新成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")
      }
    }

  }

  // 新增或修改实验室类别
  /**
* @Router post /lab/type-saveOrUpdate
* @Summary 新增或修改实验室类别
* @Description labName 为必传项
* @Request body cuType
* @apikey
*/
  async saveOrUpdateType() {
    let { ctx, service } = this
    let { typeId, typeName } = ctx.request.body

    let res = await service.laboratory.saveOrUpdateType(typeName, typeId)
    if (res == "添加成功" || res == "修改成功") {
      ctx.body = new Response(200, res, "")
    } else {
      ctx.body = new Response(403, res, "")
    }

  }
    /**
* @Router post /lab/check-typeOnly
* @Summary 检查实验室类别唯一性
* @Description typeName 为必传项
* @Request body checkOnly
*/
  async checkTypeOnly(){
    let { ctx, service } = this
    let { typeName } = ctx.request.body
    let res = await service.laboratory.checkTypeOnly(typeName)
    ctx.body = new Response(200,res,"")
  }
  // 创建实验室当日可预约记录
  /**
* @Router post /lab/create-dayTime
* @Summary 创建或获取实验室当日可预约时间段(配合日历组件使用)
* @Description dateTime、laboratoryId 为必传项
* @Request body createDayTime
* @apikey
*/
  async createDayTime() {
    let { ctx, service } = this
    let { dateTime, laboratoryId } = ctx.request.body

    if (dateTime == undefined || laboratoryId == undefined) {
      ctx.body = new Response(400, "必填字段dateTime或laboratroyId不能为空", "")
      return
    }
    let res = await service.laboratory.createDayTime(dateTime, laboratoryId)
    ctx.body = new Response(200, res, "")
  }
  /**
 * @Router get /laboratory/getByPage
 * @Summary 分页查询实验室(弃用)
 * @Description 其中pageNo和pageSize为必填项
 * @Request query number *pageNo
 * @Request query number *pageSize
 * @Request query string labName
 * @apikey
 */
  async getLabPage() {
    let { ctx, service } = this
    let params = ctx.query
    let { pageNo, pageSize } = params
    if (!pageNo || !pageSize) {
      ctx.body = new Response(403, { result: "缺少必填项" }, "操作失败")
    } else {
      let res = await service.laboratory.getLabPage(params)
      ctx.body = new Response(200, res, "操作成功")
    }


  }
  // 获取实验室详情
  /**
 * @Router get /lab/detail-lab
 * @Summary 获取实验室详情
 * @Description laboratoryId为必填项
 * @Request query number *laboratoryId
 * @apikey
 */
  async getLabDetail() {
    let { ctx, service } = this
    let { laboratoryId } = ctx.query
    laboratoryId = parseInt(laboratoryId)
    let res = await service.laboratory.getLabDetail(laboratoryId)
    if (res.message == "查询成功") {
      this.ctx.body = new Response(200, res, "")
    } else {
      this.ctx.body = new Response(400, res.message, "")
    }
  }
  // 条件获取实验室列表
  /**
* @Router get /lab/list-lab
* @Summary 条件获取实验室列表
* @Description 可传条件为 labName 、typeId 什么都不传默认查全部
* @Request query string labName
* @Request query number typeId
* @Request query number laboratoryId
* @apikey
*/
  async getLabList() {
    let { ctx, service } = this
    let { labName, address, typeId, isExamine,laboratoryId} = ctx.query

    let res = await service.laboratory.getLabList(labName, address, typeId, isExamine,laboratoryId)
    ctx.body = new Response(200, res, "")
  }

  // 删除实验室
  /**
* @Router delete /lab/del-lab
* @Summary 删除实验室
* @Description laboratoryId为必填项
* @Request query number *laboratoryId
* @apikey
*/
  async delLab() {
    let { ctx, service } = this
    let { laboratoryId } = ctx.request.body
    if (laboratoryId == undefined) {
      ctx.body = new Response(400, "缺少必填字段", "")
      return
    } else {
      laboratoryId = parseInt(laboratoryId)
      let res = await service.laboratory.delLab(laboratoryId)
      if (res == "删除成功") {
        ctx.body = new Response(200, res, "")
      } else {
        ctx.body = new Response(403, res, "")
      }
    }
  }
  // 删除实验室类别
  /**
* @Router delete /lab/type-del
* @Summary 删除实验室类别
* @Description typeId为必填项
* @Request query number *typeId
* @apikey
*/
  async deleteType() {
    let { ctx, service } = this
    let { typeId } = ctx.request.body
    if (typeId == undefined) {
      ctx.body = new Response(400, "缺少必填字段",)
    } else {
      let res = await service.laboratory.deleteType(typeId)
      if (res == "删除成功") {
        ctx.body = new Response(200, res, "")

      } else {
        ctx.body = new Response(403, res, "")

      }
    }
  }
  // 取消收藏实验室 其实就是get
  /**
 * @Router delete /lab/collect-lab-del
 * @Summary 取消收藏实验室
 * @Description userId 、laboratoryId为必填项
 * @Request query string *userId
 * @Request query number *laboratoryId
 * @apikey
 */
  async collectLabDel() {
    let { ctx, service } = this
    let { userId, laboratoryId } = ctx.request.body

    if (!userId || !laboratoryId) {
      ctx.body = new Response(400, "取消userId或laboratoryId", "")
    } else {
      let res = await service.laboratory.collectLabDel(userId, laboratoryId)
      if (res == "取消成功") {
        ctx.body = new Response(200, res, "")

      } else {
        ctx.body = new Response(403, res, "")

      }
    }
  }

  //   获取所有实验室类别
    /**
* @Router get /lab/lab-type-all
* @Summary 条件获取实验室类别
* @Description 可传条件为 typeName 、typeId 什么都不传默认查全部
* @Request query string typeName
* @Request query number typeId
* @apikey
*/
  async getAllType() {
    let { ctx, service } = this
    let { typeName, typeId} = ctx.query

    let result = await service.laboratory.getAllType(typeName,typeId)
    ctx.body = new Response(200, result, "")
  }


}
module.exports = LabController